﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DormMS.IService;
using DormMS.IService.Common;
using DormMS.Model;
using DormMS.Model.SearchModels;
using Infrastructure;
using NLog;

namespace DormMS.Controllers
{
    public class StudentBedController : BaseController
    {
        protected IRoomService roomService;
        protected ILogService logService;
        protected IUserInfoService userInfoService;
        protected IStudentBedService studentBedService;
        protected IBuildingService buildingService;
        protected IDepartmentService departmentService;
        protected IClassService classService;
        protected IBedService bedService;
        private static Logger NLog = LogManager.GetLogger("Request");
        /// <summary>
        /// 初期化
        /// </summary>
        /// <param name="s"></param>
        /// <param name="l"></param>
        /// <param name="_studentBedService"></param>
        /// <param name="_roomService"></param>
        /// <param name="_buildingService"></param>
        /// <param name="_departmentService"></param>
        /// <param name="_classService"></param>
        /// <param name="_bedService"></param>
        public StudentBedController(IUserInfoService s, ILogService l, IStudentBedService _studentBedService, IRoomService _roomService, IBuildingService _buildingService, IDepartmentService _departmentService, IClassService _classService, IBedService _bedService) : base(s)
        {
            userInfoService = s;
            logService = l;
            studentBedService = _studentBedService;
            roomService = _roomService;
            buildingService = _buildingService;
            departmentService = _departmentService;
            classService = _classService;
            bedService = _bedService;
        }
        /// <summary>
        /// 获取学生对应床位列表
        /// </summary>
        /// <returns></returns>
        // GET: Student
        public ActionResult StudentBedList()
        {
            return View();
        }
        /// <summary>
        /// 获取学生对应床位初期化列表
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public ActionResult GetStudentBedList(StudentBedSearchModel search)
        {
            int total = 0;
            var list = studentBedService.GetStudentBedBySearch(search, ref total);
            return Json(new { Total = total, Results = list, _UserInfo = userInfoService.GetUserInfo(CurrentLoginUser.Id), BuildingList = buildingService.GetBuildingAllList(), RoomList = roomService.GetRoomAllList(), DepartmentList = departmentService.getDepartmentList(), ClassList = classService.getClassAllList(), TeacherList = userInfoService.GetUserInfoByisTeacher(1) });
        }
        /// <summary>
        /// 获取学生对应床位明细
        /// </summary>
        /// <returns></returns>
        public ActionResult StudentBedDetail()
        {
            return View();
        }
        /// <summary>
        /// 获取学生对应床位初期化明细
        /// </summary>
        /// <returns></returns>
        public ActionResult GetStudentBedInitDetail(int id)
        {

            var studentBed = studentBedService.GetStudentBedById(id);
            if (studentBed == null)
            {
                studentBed = new StudentBed();
                studentBed.StudentId = 0;
                studentBed.BedId = 0;
            }
            var bedInfo = bedService.GetBedById(studentBed.BedId);
            if(bedInfo == null)
            {
                bedInfo = new Bed();
                bedInfo.RoomId = 0;
                bedInfo.BuildingId = 0;
            }

            var buildingInfo = buildingService.GetBuildingInfoById(bedInfo.BuildingId);
            if (buildingInfo == null)
            {
                buildingInfo = new Building();
                buildingInfo.Id = 0;
            }

            var roomInfo = roomService.GetRoomInfoById(bedInfo.RoomId);
            if (roomInfo == null)
            {
                roomInfo = new Room();
                roomInfo.Id = 0;
            }
            
            return Json(new { StudentBed = studentBed, BedInfo = bedInfo ,RoomInfo = roomInfo,BuildingInfo = buildingInfo, BuildingList = buildingService.GetBuildingAllList(), RoomList = roomService.GetRoomAllList(), DepartmentList = departmentService.getDepartmentList(), ClassList = classService.getClassAllList(), TeacherList = userInfoService.GetUserInfoByisTeacher(1),StudentInfo = userInfoService.GetUserInfoById(studentBed.StudentId),BedList = bedService.GetBedListByRoomID(studentBed.RoomId) });
        }
        /// <summary>
        /// 通过RoomID获取床位号和柜号
        /// </summary>
        /// <param name="RoomID"></param>
        /// <returns></returns>
        public ActionResult GetBedInfo(int RoomId)
        {

            return Json(new { BedList = bedService.GetBedListByRoomID(RoomId)});
        }
        /// <summary>
        /// 学生对应床位保存CHECK
        /// </summary>
        /// <param name="BedId"></param>
        /// <returns></returns>
        public ActionResult StudentBedCheck(int BedId)
        {
            Result result = new Result();
            result.isSuccess = false;
            var entity = bedService.GetBedById(BedId);
            
            if(entity.Status == 1)
            {
                result.isSuccess = true;
            }
            else
            {

            }
            
            
            return Json(result);

        }
        /// <summary>
        /// 保存学生对应床位
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult StudentBedSave(StudentBed model)
        {
            Result result = new Result();
            try
            {
                var entity = studentBedService.GetStudentBedById(model.Id);
                var newbedentity = bedService.GetBedById(model.BedId);
                newbedentity.Status = 1;
                bedService.BedEdit(newbedentity);

                int rstcount = 0;
                if (entity == null)
                {
                    model.AddUserId = CurrentLoginUser.Id;
                    model.AddTime = DateTime.Now;
                    rstcount = studentBedService.StudentBedAdd(model);
                    result.isSuccess = true;
                }
                else
                {
                    var oldbedentity = bedService.GetBedById(entity.BedId);
                    
                    oldbedentity.Status = 0;
                    bedService.BedEdit(oldbedentity);

                    entity.StudentId = model.StudentId;
                    entity.BuildingId = model.BuildingId;
                    entity.RoomId = model.RoomId;
                    entity.BedId = model.BedId;                    
                    entity.DepartmentId = model.DepartmentId;
                    entity.TeacherId = model.TeacherId;
                    entity.ClassId = model.ClassId;
                    entity.EditUserId = CurrentLoginUser.Id;
                    entity.EditTime = DateTime.Now;
                    rstcount = studentBedService.StudentBedEdit(model, null);
                    result.isSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.msg = "保存学生对应床位信息失败！";
                NLog.Debug("保存学生对应床位信息失败：" + ex.Message + "，" + ex.StackTrace);
            }
            return Json(result);

        }

        /// <summary>
        /// 删除学生对应床位信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult StudentBedDel(StudentBed model)
        {
            Result result = new Result();
            var entity = studentBedService.GetStudentBedById(model.Id);

            if (entity != null)
            {
                model.IsDeleted = true;
                studentBedService.StudentBedDel(model);
                result.isSuccess = true;
            }
            else
            {
                result.msg = "公寓信息不存在！";
            }
            return Json(result);
        }

        /// <summary>
        /// 通学号查找学生信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetStudentInfo(string StudentNO)
        {
            Result result = new Result();
            result.isSuccess = false;
            var data = userInfoService.GetUserInfoByStudentNO(StudentNO);
            if(data == null)
            {
                
            }
            else
            {
                result.isSuccess = true;
                result.data = data;
            }

            return Json(result);
        }
    }
}